// pages/applyRefund/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resData: '',
    show: false,
    columns: [],
    orderId: '',
    reason: '',
    reasonArr: '',
    reasonId: '',
    uploadedFile: [],
    instruction: '',
    refundMoney: '',
    isShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      orderId: options.id
    })
    util.http({
      url: 'api/miniwechat/order/find_order',
      data: {
        order_id: options.id
      }
    }).then(res => {
      that.setData({
        resData: res
      })
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })

  },

  onInput (e) {
    let val = e.detail.value
    if (Number(e.detail.value) > Number(this.data.resData.sum_money)) {
      this.setData({
        refundMoney: this.data.resData.sum_money
      })
    } else {
      this.setData({
        refundMoney: e.detail.value
      })
    }
    // val = val.replace(/[^\d.]/g, "").replace(/^\./g, "").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
    // if (val.indexOf(".") > -1) {
    //   val = val.substring(0, val.indexOf(".") + 3)
    // }
    // if (parseFloat(val) > this.data.resData.sum_money) {
    //   val = val.slice(0, val.length - 1)
    // }
    // this.setData({
    //   refundMoney: val
    // })
  },

  refundPopup () {
    let that = this
    util.http({
      url: 'api/miniwechat/center/category',
      data: {
        type: "Refund"
      }
    }).then(res => {
      // 
      console.log(res)
      const reason = []
      res.forEach((item, index) => {
        reason.push(item.name)
      })
      // 
      console.log(reason)
      that.setData({
        columns: reason,
        reasonArr: res
      })
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })

    this.setData({
      isShow: false,
      show: true
    })
  },

  onClose () {
    this.setData({
      isShow: true,
      show: false
    })
  },

  onCancel () {
    this.onClose()
  },

  onConfirm (even) {
    let index = even.detail.index;
    console.log(even)
    this.setData({
      reason: even.detail.value,
      reasonId: this.data.reasonArr[index].id
    })
    this.onClose()
  },

  imgChange() {
    if (this.data.uploadedFile && this.data.uploadedFile.length < 6) {
      let that = this
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          util.postImg({
            url: 'api/miniwechat/index/upload',
            file: tempFilePaths[0]
          }).then((imgData) => {
            let imgInfo = JSON.parse(imgData)
            let urlArr = that.data.uploadedFile
            let mark = true
            urlArr.forEach((item, index) => {
              if (item == imgInfo.data.url) mark = false
            })
            if(mark) urlArr.push(imgInfo.data.url)
            that.setData({
              uploadedFile: urlArr
            })
          }).catch((err) => {
            wx.showToast({
              title: String(err),
              icon: 'none',
              duration: 2000
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: "最多上传6张图片",
        icon: 'none',
        duration: 2000
      })
    }
  },

  delImgFils (e) {
    let url = e.currentTarget.dataset.url
    let arr = this.data.uploadedFile
    arr.forEach((item, index) => {
      if (item == url) {
        arr.splice(index, 1)
      }
    })
    this.setData({
      uploadedFile: arr
    })
  },

  refundInstruction (e) {
    this.setData({
      instruction: e.detail.value
    })
  },

  onConfirmData () {
    if (!this.data.refundMoney || this.data.refundMoney == 0) {
      wx.showToast({
        title: "退款金额有误",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!this.data.reason) {
      wx.showToast({
        title: "请选择退款原因",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!this.data.instruction) {
      wx.showToast({
        title: "请填写退款说明",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    let data = {
      user_id: util.isLogin().user_id,
      order_id: this.data.orderId,
      refund_money: parseFloat(this.data.refundMoney),
      refund_content: this.data.instruction,
      refund_image: this.data.uploadedFile.join(","),
      refund_type_id: this.data.reasonId
    }
    let that = this
    util.http({
      url: 'api/miniwechat/order/record_create',
      method: 'POST',
      data: data
    }).then(res => {
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 1000
      })
      wx.navigateBack({
        delta: 1
      })
      setTimeout(() => {
        if (this.data.resData.status == 1) {
          wx.navigateTo({
            url: '/pages/refund/index?id=' + this.data.orderId
          })
        } else {
          wx.reLaunch({
            url: '/pages/index/index?id=3'
          })
        }
      }, 1000)
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})